package com.edu.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.edu.entity.Conditions;
import com.edu.entity.Goods;
import com.edu.entity.GoodsToWeb;
import com.edu.entity.User;
import com.edu.entity.userFollow;
import com.edu.service.impl.GoodServerImpl;
import com.edu.service.impl.myFollowServiceImpl;

/**
 * Servlet implementation class sellerInfoServlet
 */
@WebServlet(name = "sellerInfo", urlPatterns = { "/sellerInfo" })
public class sellerInfoServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		GoodServerImpl gsi = new GoodServerImpl();
		HttpSession session = request.getSession();
		User u = (User) session.getAttribute("logInUser");
		GoodsToWeb gtw = (GoodsToWeb) session.getAttribute("good");
		Conditions con = null;
		Goods g = new Goods();
		g.setSellerId(gtw.getSellerId());
		ArrayList<GoodsToWeb> list = gsi.indexFindGoods(g, con);
		System.out.println(list.size());
		int rowNum = list.size() / 4;
		boolean hasFollow = false;
		if(list.size() % 4 != 0) {
			rowNum++;
		}
		myFollowServiceImpl msi = new myFollowServiceImpl();
		userFollow uf = msi.findFollow(u.getUserid(), gtw.getSellerId());
		System.out.println(uf.getUserId());
		if(uf.getUserId() != 0) {
			hasFollow = true;
			System.out.println("此用户被关注"+uf.getUserId());
			
		}else {
			hasFollow = false;
			System.out.println("此用户未关注");
		}
		request.setAttribute("hasFollow", hasFollow);
		request.setAttribute("goodsList", list);
		request.setAttribute("rowNum", rowNum);
		request.getRequestDispatcher("sellerInfo.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		GoodServerImpl gsi = new GoodServerImpl();
		String getId = request.getParameter("sellerid");
		int sellerid = Integer.parseInt(getId);
		String sellername = request.getParameter("sellername");
		System.out.println(sellerid+","+sellername);
		Conditions con = null;
		Goods g = new Goods();
		g.setSellerId(sellerid);
		ArrayList<GoodsToWeb> list = gsi.indexFindGoods(g, con);
		int rowNum = list.size() / 4;
		if(list.size() % 4 != 0) {
			rowNum++;
		}
		request.setAttribute("goodsList", list);
		request.setAttribute("good", list.get(0));
		request.setAttribute("rowNum", rowNum);
		request.getRequestDispatcher("sellerInfo.jsp").forward(request, response);
	}

}
